package GUI;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;
import javax.swing.JTextPane;
import javax.swing.JLabel;

import java.awt.Desktop;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

import client.ClientConsole;

import Library.Books;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author mishel hen
 *
 */
/**
 * This class for display the search results
 */
public class GUI_ShowBooks extends JPanel{
	/**
	 * client Connected
	 */
	private ClientConsole clc;
	/**
	 * table
	 */
	private JTable table_3;
	/**
	 * Text field for bookId
	 */
	private JTextField textField;
	/**
	 * list for books
	 */
	private ArrayList<Books> books;
	/**
	 * File
	 */
	private File pdfFile;

	/**
	 * @param cc
	 * @param booksList 
	 * @param text 
	 * @param gs 
	 */
	public GUI_ShowBooks( ClientConsole cc, ArrayList<Books> booksList, String text, final GUI_Search gs) {
	
				setLayout(null);
				//this.userName=userName;
				this.setSize(600, 600);
				clc=cc;
				books=booksList;
				
				
				JLabel lblResults = new JLabel("Results for: " + text);
				lblResults.setFont(new Font("Tahoma", Font.PLAIN, 20));

				lblResults.setBounds(95, 20, 186, 25);
				add(lblResults);
									
				JButton btnBack = new JButton("Back");
				
				btnBack.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						clc.cgui.setContentPane(gs);
					}
				})
				;
				btnBack.setBounds(95, 370, 100, 30);
				add(btnBack);
				
				showResults();
				
			}
			
			
			public void showResults() {
				
				
				JScrollPane scrollPane = new JScrollPane();
				scrollPane.setBounds(95, 70, 500, 300);
				add(scrollPane);
				
			
				
				table_3 = new JTable(books.size(),7);
				scrollPane.setViewportView(table_3);
				
				
				JLabel lblBookId = new JLabel("Book id");
				lblBookId.setBounds(285, 378, 46, 14);
				add(lblBookId);
				
				textField = new JTextField();
				textField.setBounds(349, 375, 86, 20);
				add(textField);
				textField.setColumns(10);
				
				JButton btnShowContent = new JButton("Show Content");
				btnShowContent.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String t=textField.getText();
						String[] s={"get content",t};
						try {
							clc.client.sendToServer(s);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						while(clc.client.flag==0);
						clc.client.flag=0;
						byte[] fileBytes=(byte[]) clc.client.message;
						
						Desktop d=Desktop.getDesktop();
						FileOutputStream targetFile;
						System.out.println(fileBytes.length);
						try {
							String target="c:/files/tempContent.pdf";
							targetFile = new FileOutputStream(target);
							targetFile.write(fileBytes);
				            targetFile.close();
				            File f=new File(target);
				            d.open(f);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					
						}
					
					}
				});
				btnShowContent.setBounds(462, 374, 128, 23);
				add(btnShowContent);
							
							
				
							
				table_3.getColumnModel().getColumn(0).setHeaderValue("Name");
				table_3.getColumnModel().getColumn(1).setHeaderValue("bookId");
				table_3.getColumnModel().getColumn(2).setHeaderValue("Author");
				table_3.getColumnModel().getColumn(3).setHeaderValue("Edition");
				table_3.getColumnModel().getColumn(4).setHeaderValue("Scope");
				table_3.getColumnModel().getColumn(5).setHeaderValue("Shelf Location");
				table_3.getColumnModel().getColumn(6).setHeaderValue("availabile date");

				
				
				for(int i=0;i<books.size();i++)
				{
					
						table_3.setValueAt(books.get(i).getBookName(),i,0);
						table_3.setValueAt(books.get(i).getBookId(),i,1);
						table_3.setValueAt(books.get(i).getAuthor(),i,2);
						table_3.setValueAt(books.get(i).getEdition(),i,3);
						table_3.setValueAt(books.get(i).getScope(),i,4);
						table_3.setValueAt(books.get(i).getShelfLocation(),i,5);
						table_3.setValueAt(books.get(i).getReturnDate(),i,6);


				}
				
				
				
				
			
			}
		


}
